﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VQWEB.common
{
 
    public class SecurityHttpmodule : IHttpModule
    {
        public SecurityHttpmodule() { }
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication application)
        {
            application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));

        }

        void application_AuthenticateRequest(object sender, EventArgs e)
        {

        }
        private void Application_BeginRequest(object source, EventArgs e)
        {
            // Create HttpApplication and HttpContext objects to access
            // request and response properties.
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            string filePath = context.Request.FilePath;
            string fileExtension = VirtualPathUtility.GetExtension(filePath);
            if (fileExtension.Equals(".aspx"))
            {
                context.Response.Write("<h1><font color=red>" +
                    "404: Not Found" +
                    "</font></h1>");

                context.Response.End();

            }

            //string path = HttpContext.Current.Request.Url.AbsolutePath;
            string url = context.Request.Url.AbsolutePath;

            if (url.IndexOf("/admin") == 0 && !url.Equals("/admin/login"))
            {
                //context.Response.Write("Truy cap admin: " + path.IndexOf("/admin"));                
                //context.Response.Redirect("/admin/login");
                //context.Response.End();
            }

        }
    }
}